/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.AcademicYearDTO;
import java.sql.ResultSet;

/**
 *
 * @author trungpv
 */
public class AcademicYearDAL {
     
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private AcademicYearDTO m_AcademicYearObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    public AcademicYearDAL() {
        this.m_AcademicYearObj = new AcademicYearDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }

    /**
     * Ham lay tat ca cac Nam Hoc
     *
     * @return boolean
     */
    public ResultSet getAllAcademicYear() {
       try
        {
            m_sqlCmd = "{Call GetAllAcademicYearFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    /**
     * Ham lay Nam Hoc theo ID Nam Hoc
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getAcademicYearById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetAcademicYearByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getMaxAcademicYear()
    {
        try {
            m_sqlCmd = "{Call GetMaxAcademicYearFromDatabase}";
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null; 
    }
     /**
     * ham them moi Nam Hoc xuong database
     * @param _academicyear
     * @return true: success, false: fail
     */
    public boolean insertAcademicYearToDatabase(AcademicYearDTO _academicyear)
    {
       try {
           m_sqlCmd = "{Call InsertAcademicYearToDatabase(?,?)}";
           String[] arrStr = {_academicyear.getAcademicYearID(), _academicyear.getNameOfAcademicYear()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin Nam Hoc
     * @param _academicyear
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateAcademicYearToDatabase(AcademicYearDTO _academicyear)
    {
       try {
           m_sqlCmd = "{Call UpdateAcademicYearToDatabase(?,?)}";
           String[] arrStr = {_academicyear.getAcademicYearID(), _academicyear.getNameOfAcademicYear()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham xoa mot Nam Hoc ra khoi database
     * @param _academicyear
     * @return 
     */
     public boolean deleteAcademicYearFromDatabase(AcademicYearDTO _academicyear)
    {
        try {
           m_sqlCmd = "{Call DeleteAcademicYearFromDatabase(?)}";
           String[] arrStr = {_academicyear.getAcademicYearID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
